define(['jquery', 'bootstrap', 'backend', 'table', 'form'], function ($, undefined, Backend, Table, Form) {

    var Controller = {
        index: function () {
            // 初始化表格参数配置
            Table.api.init({
                extend: {
                    index_url: 'circle/index' + location.search,
                    add_url: 'circle/add',
                    edit_url: 'circle/edit',
                    del_url: 'circle/del',
                    multi_url: 'circle/multi',
                    import_url: 'circle/import',
                    table: 'user_circle',
                }
            });

            var table = $("#table");

            // 初始化表格
            table.bootstrapTable({
                url: $.fn.bootstrapTable.defaults.extend.index_url,
                pk: 'id',
                sortName: 'id',
                fixedColumns: true,
                fixedRightNumber: 1,
                columns: [
                    [
                        {checkbox: true},
                        {field: 'id', title: __('Id')},
                        // {field: 'user_id', title: __('User_id')},
                        // {field: 'cate_id', title: __('Cate_id')},
                        // {field: 'vehicle_id', title: __('Vehicle_id')},
                        // {field: 'province_id', title: __('Province_id')},
                        // {field: 'city_id', title: __('City_id')},
                        // {field: 'area_id', title: __('Area_id')},
                        {field: 'user.username', title: __('User.username'), operate: 'LIKE', table: table, class: 'autocontent', formatter: Table.api.formatter.content},
                        {field: 'user.tel', title: __('User.tel'), operate: 'LIKE'},
                        {field: 'title', title: __('Title'), operate: 'LIKE', table: table, class: 'autocontent', formatter: Table.api.formatter.content},
                        {field: 'vehicle.name', title: __('Vehicle.name'), operate: 'LIKE', table: table, class: 'autocontent', formatter: Table.api.formatter.content},
                        {field: 'carcategory.name', title: __('Carcategory.name'), operate: 'LIKE', table: table, class: 'autocontent', formatter: Table.api.formatter.content},
                        {field: 'province.name', title: __('Province.name'), operate: false},
                        {field: 'city.name', title: __('City.name'), operate: false},
                        {field: 'area.name', title: __('Area.name'), operate: false},
                        {field: 'image', title: __('Image'), operate: false, events: Table.api.events.image, formatter: Table.api.formatter.image},
                        {field: 'price', title: __('Price'), operate: false},
                        {field: 'base_price', title: __('Base_price'), operate: false},
                        {field: 'istop', title: __('是否置顶'), searchList: {"0":__('否'),"1":__('首页置顶'),"2":__('分类置顶'),"3":__('首页/分类置顶')},visible:false},
                        {field: 'is_index_top', title: __('首页是否置顶'), operate: false},
                        {field: 'is_cate_top', title: __('分类是否置顶'), operate:false},
                        // {field: 'index_top', title: __('Index_top')},
                        // {field: 'index_top_time', title: __('Index_top_time'), operate:'RANGE', addclass:'datetimerange', autocomplete:false, formatter: Table.api.formatter.datetime},
                        // {field: 'index_top_endtime', title: __('Index_top_endtime'), operate:'RANGE', addclass:'datetimerange', autocomplete:false, formatter: Table.api.formatter.datetime},
                        // {field: 'index_top_price', title: __('Index_top_price'), operate:'BETWEEN'},
                        // {field: 'cate_top', title: __('Cate_top')},
                        // {field: 'cate_top_time', title: __('Cate_top_time'), operate:'RANGE', addclass:'datetimerange', autocomplete:false, formatter: Table.api.formatter.datetime},
                        // {field: 'cate_top_endtime', title: __('Cate_top_endtime'), operate:'RANGE', addclass:'datetimerange', autocomplete:false, formatter: Table.api.formatter.datetime},
                        // {field: 'cate_top_price', title: __('Cate_top_price'), operate:'BETWEEN'},
                        // {field: 'issale', title: __('Issale'), searchList: {"1":__('Issale 1'),"2":__('Issale 2')}, formatter: Table.api.formatter.normal},
                        {field: 'authen', title: __('Authen'), searchList: {"1":__('Authen 1'),"2":__('Authen 2'),"3":__('Authen 3')}, formatter: Table.api.formatter.normal},
                        // {field: 'reason', title: __('Reason'), operate: 'LIKE', table: table, class: 'autocontent', formatter: Table.api.formatter.content},
                        {field: 'isshow', title: __('Isshow'), searchList: {"1":__('Isshow 1'),"2":__('Isshow 2')}, formatter: Table.api.formatter.normal},
                        {field: 'isshow_pt', title: __('Isshow_pt'), searchList: {"1":__('Isshow_pt 1'),"2":__('Isshow_pt 2')}, formatter: Table.api.formatter.normal},
                        // {field: 'pt_reason', title: __('Pt_reason'), operate: 'LIKE', table: table, class: 'autocontent', formatter: Table.api.formatter.content},
                        // {field: 'pt_time', title: __('Pt_time'), operate:'RANGE', addclass:'datetimerange', autocomplete:false, formatter: Table.api.formatter.datetime},
                        // {field: 'view', title: __('View')},
                        // {field: 'collect_count', title: __('Collect_count')},
                        {field: 'add_time', title: __('Add_time'), operate:'RANGE', addclass:'datetimerange', autocomplete:false, formatter: Table.api.formatter.datetime},
                        
                        
                        // {field: 'operate', title: __('Operate'), table: table, events: Table.api.events.operate, formatter: Table.api.formatter.operate}
                        {field: 'operate', title: __('Operate'), table: table, events: Table.api.events.operate,
                        buttons: [
                            {
                                name: 'edit',
                                text: __('查看'),
                                title: __('查看'),
                                classname: 'btn btn-xs btn-primary btn-dialog',
                                icon: 'fa fa-list',
                                extend: 'data-area=\'["70%", "60%"]\'',
                                url: 'circle/edit',
                            },
                            {
                                name: 'edit',
                                text: __('支付记录'),
                                title: __('支付记录'),
                                classname: 'btn btn-xs btn-primary btn-dialog',
                                icon: 'fa fa-list',
                                extend: 'data-area=\'["70%", "60%"]\'',
                                url: 'usercircleprice/index',
                            }
                            
                        ], 
                        formatter: Table.api.formatter.operate}
                    ]
                ]
            });

            // 为表格绑定事件
            Table.api.bindevent(table);
        },
        add: function () {
            Controller.api.bindevent();
        },
        edit: function () {
            $(document).on("change", "#c-authen", function(){
                var authen=$("#c-authen").val();
                // alert(authen);
                if(authen=='3')
                {
                    $("#reason").show();
                }
                else
                {
                    $("#reason").hide();
                }

            });
            $(document).on("change", "#c-isshow_pt", function(){
                var isshow_pt=$("#c-isshow_pt").val();
                // alert(authen);
                if(isshow_pt=='2')
                {
                    $("#pt_reason").show();
                }
                else
                {
                    $("#pt_reason").hide();
                }

            });
            var input_select_hide = true;
            $(document).on("focus", '#c-reason', function() {
                $("#reason_input_select").show();
            })
            $(document).on("blur", '#c-reason', function() {
                if (input_select_hide) {
                    $("#reason_input_select").hide();
                }
            })
            $(document).on("mouseenter", '#reason_input_select', function() {
                input_select_hide = false;
            })
            $(document).on("mouseleave", '#reason_input_select', function() {
                input_select_hide = true;
            })
            $(document).on("click", '.reason_input_select_li', function() {
                // 获取选中的值
                var selectedValue = $(this).text();
                // 设置input元素的值
                $('#c-reason').val(selectedValue); // 或者使用 selectedText
                $("#reason_input_select").hide();
            });
            Controller.api.bindevent();
        },
        api: {
            bindevent: function () {
                Form.api.bindevent($("form[role=form]"));
            }
        }
    };
    return Controller;
});
